define('framework/validation/ValidationHolder', [
    'require',
    'config.properties'
], function (require, config) {

    var paths = config.$paths.$current;

    return function () {

        function getResourcePaths(groupName) {
            return [
                paths.ctx + '/static/app/validation/generated/' + groupName + ".js",
                'i18n!' + paths.ctx + '/static/app/nls/entity-' + groupName + ".js"
            ];
        }

        return {
            $rulesAndI18ns: {},

            /**
             *
             * @param groupName {String} 校验分组名称
             * @param callback {function({rules:Object<String, ?>,i18ns:Array<String, String>}, async:boolean)} 获取到校验规则和多语言错误消息后回调
             */
            loadRulesAndI18ns: function (groupName, callback) {
                var me = this,
                    rulesAndI18ns = me.$rulesAndI18ns,
                    resourcePaths;
                if (rulesAndI18ns.hasOwnProperty(groupName)) {
                    callback(rulesAndI18ns[groupName], false);
                } else {
                    resourcePaths = getResourcePaths(groupName);
                    require(resourcePaths, function (rulesContent, i18ns) {
                        rulesAndI18ns[groupName] = {
                            rules: rulesContent.rules,
                            i18ns: i18ns
                        };
                        callback(rulesAndI18ns[groupName], true);
                    });
                }
            },

            /**
             *
             * @param groupName {String} 校验分组名称
             * @param callback {function(rules:Object<String, ?>, async:boolean)} 获取到校验规则后回调
             * @return {boolean} 是否已`load`过
             */
            loadRules: function (groupName, callback) {
                this.loadRulesAndI18ns(groupName, function (rulesAndI18ns, async) {
                    callback(rulesAndI18ns.rules, async);
                });
            },

            /**
             *
             * @param groupName {String} 校验分组名称
             * @param callback {function(i18ns:Object<String, String>, async:boolean)} 获取到多语言错误消息后回调
             * @return {boolean} 是否已`load`过
             */
            loadI18ns: function (groupName, callback) {
                this.loadRulesAndI18ns(groupName, function (rulesAndI18ns, async) {
                    callback(rulesAndI18ns.i18ns, async);
                });
            }
        };
    };
});